package com.wash.shoes.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.wash.shoes.domain.dto.ShopStsXcxDto;
import com.wash.shoes.domain.vo.StsOrderVo;
import com.wash.shoes.exception.WashException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lijianguo
 * @Date 11/03/2025 20:05
 * 请输入类的简介
 **/
public class StsDateInfoUtil {


    public static List<StsOrderVo> processEmptyDays(List<StsOrderVo> daySts, ShopStsXcxDto shopStsXcxDto) {
        List<StsOrderVo> allDaySts = new ArrayList<>();
        List<String> dateStringList = getDatesBetween(shopStsXcxDto.getStartTime(), shopStsXcxDto.getEndTime());
        for (String dateString : dateStringList) {
            StsOrderVo oneDayInfo = null;
            for (StsOrderVo dayStsOrderVo : daySts) {
                if (dateString.equals(dayStsOrderVo.getDateStr())) {
                    oneDayInfo = dayStsOrderVo;
                    break;
                }
            }
            // 判断是不是存在
            if (oneDayInfo == null) {
                oneDayInfo = new StsOrderVo();
                oneDayInfo.setDateStr(dateString);
                oneDayInfo.setTotalPrice(0.0);
                oneDayInfo.setOrderPrice(0.0);
                oneDayInfo.setShoeNum(0L);
                oneDayInfo.setOrderNum(0);
            }
            allDaySts.add(oneDayInfo);
        }
        return allDaySts;
    }


    public static void processDate(ShopStsXcxDto shopStsXcxDto) {
        if (StrUtil.isBlank(shopStsXcxDto.getStartTime())) {
            shopStsXcxDto.setStartTime(DateUtil.beginOfWeek(new Date()).getTime() + "");
        }
        if (StrUtil.isBlank(shopStsXcxDto.getEndTime())) {
            shopStsXcxDto.setEndTime(new Date().getTime() + "");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime;
        Date endTime;
        try {
            startTime = sdf.parse(shopStsXcxDto.getStartTime());
            endTime = sdf.parse(shopStsXcxDto.getEndTime());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        long days = DateUtil.betweenMs(endTime, startTime) / (1000 * 3600 * 24);
        if (days > 35) {
            throw new WashException("时间不能超过一个月");
        }
    }

    public static List<String> getDatesBetween(String startDateStr, String endDateStr) {
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 将字符串转换为 LocalDate 对象
        LocalDate startDate = LocalDate.parse(startDateStr.substring(0, 10), formatter);
        LocalDate endDate = LocalDate.parse(endDateStr.substring(0, 10), formatter);
        // 存储日期的列表
        List<String> dates = new ArrayList<>();
        // 从开始日期开始，逐天增加，直到结束日期
        for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) {
            // 将日期转换为字符串并添加到列表中
            dates.add(date.format(formatter));
        }
        return dates;
    }
}
